package com.oncliedisk.administrator.ui;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.mogujie.tt.R;
import com.oncliedisk.administrator.model.BaseBean;


import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2015/12/1 0001.
 */
public abstract class BaseUploadActivity extends Activity {
    /***
     * 记录选择的条目
     */
    private List<BaseBean> mSelected = new ArrayList<BaseBean>();
    /**
     * 扫描到本地的所有bean
     */
    protected List<BaseBean> data;
    protected ListView listview;
    protected BaseAdapter adapter ;
    protected LinearLayout ll_loading;
    protected Button btn_upload;
    protected Button bt_selectall;
    protected Button select_folder;
    protected ImageView iv_back;
    protected TextView tv_title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.a_uploadmedia);
        ll_loading = (LinearLayout) findViewById(R.id.ll_loading);
        listview = (ListView) findViewById(R.id.listview) ;
        btn_upload = (Button) findViewById(R.id.btn_upload);
        select_folder = (Button) findViewById(R.id.select_folder);
        bt_selectall = (Button) findViewById(R.id.bt_selectall);
        iv_back = (ImageView) findViewById(R.id.iv_back);
        tv_title = (TextView) findViewById(R.id.tv_title);
        fillData();
        setUploadTitle();
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ViewHolder holder = (ViewHolder) view.getTag();
                //如果是未选中的CheckBox,则添加动画
                if (holder.cb_select.isChecked()) {
                    holder.cb_select.setChecked(false);
                    data.get(position).setChecked(false);
                    mSelected.remove(data.get(position));
                } else {
                    addAnimation(holder.cb_select);
                    holder.cb_select.setChecked(true);
                    data.get(position).setChecked(true);
                    btn_upload.setEnabled(true);
                    btn_upload.setBackgroundDrawable(getResources().getDrawable(R.drawable.btn_upload_pressed));
                    mSelected.add(data.get(position));
                }
                if (mSelected.size() == 0) {
                    btn_upload.setEnabled(false);
                    btn_upload.setFocusable(false);
                    btn_upload.setBackgroundDrawable(getResources().getDrawable(R.drawable.btn_upload_default));
                }
            }
        });

        bt_selectall.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectAll();
            }
        });
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        btn_upload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(BaseUploadActivity.this, "点击了上传", Toast.LENGTH_LONG).show();
            }
        });
        select_folder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(BaseUploadActivity.this, "选择上传到某个文件夹", Toast.LENGTH_LONG).show();
            }
        });
    }
    public abstract void fillData();
    protected class MyAdapter extends BaseAdapter {
        private Context context;
        private List<BaseBean> data;


        public MyAdapter(Context context, List<BaseBean> data) {
            super();
            this.context = context;
            this.data = data;
        }

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            View view;
            BaseBean info = data.get(position);
            final ViewHolder holder;
            if (null != convertView) {
                view = convertView;
                holder = (ViewHolder) view.getTag();
            } else {
                view = View.inflate(BaseUploadActivity.this,R.layout.item_uploadmedia, null);
                holder = new ViewHolder();
                holder.iv_icon = (ImageView) view.findViewById(R.id.iv_icon);
                holder.cb_select = (CheckBox) view.findViewById(R.id.cb_select);
                holder.tv_filename = (TextView) view.findViewById(R.id.tv_filename);
                view.setTag(holder);
            }

            setIcon(holder,position);
            holder.tv_filename.setText(data.get(position).getDisplayName());
            holder.cb_select.setChecked(info.isChecked());
            return view;
        }
    }

    /**
     * 修改图标
     * @param holder
     * @param position
     */
    protected abstract void setIcon(ViewHolder holder,int position);

    /**
     * 设置标题
     */
    protected abstract void setUploadTitle();
    /**
     * 给CheckBox加点击动画，利用开源库nineoldandroids设置动画
     * @param view
     */
    private void addAnimation(View view){
        float [] vaules = new float[]{0.5f, 0.6f, 0.7f, 0.8f, 0.9f, 1.0f, 1.1f, 1.2f, 1.3f, 1.25f, 1.2f, 1.15f, 1.1f, 1.0f};
        AnimatorSet set = new AnimatorSet();
        set.playTogether(ObjectAnimator.ofFloat(view, "scaleX", vaules),
                ObjectAnimator.ofFloat(view, "scaleY", vaules));
        set.setDuration(150);
        set.start();
    }


    @Override
    public void onBackPressed() {
        Toast.makeText(this, "选中 " + getSelectItems()+ " 条目", Toast.LENGTH_LONG).show();
        super.onBackPressed();
    }

    /**
     * 全选
     */
    public void selectAll() {
        if (null != data) {
            if( mSelected.size()  < data.size()) {
                mSelected.clear();
                for(BaseBean info:data) {
                    info.setChecked(true);
                    mSelected.add(info);
                }
                btn_upload.setEnabled(true);
                btn_upload.setBackgroundDrawable(getResources().getDrawable(R.drawable.btn_upload_pressed));
            }else if(mSelected.size() == data.size()) {
                for(BaseBean info:data) {
                    info.setChecked(false);
                }
                mSelected.clear();
                btn_upload.setEnabled(false);
                btn_upload.setBackgroundDrawable(getResources().getDrawable(R.drawable.btn_upload_default));
            }
            if(adapter != null) {
                adapter.notifyDataSetChanged();
            }
        }
    }
    /**
     * 获取选中的Item的个数
     * @return
     */
    private int getSelectItems(){
        int count = 0;
        if(data != null) {
            for(BaseBean info:data) {
                if(info.isChecked()) {
                    count++;
                }
            }
        }
        return count;
    }

    class ViewHolder {
        ImageView iv_icon;
        TextView tv_filename;
        CheckBox cb_select;
    }

}
